package com.ztgf.shopping.service.epg;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.shopping.models.bo.epg.EpgShopMainNavigationBO;
import com.ztgf.shopping.models.constant.ShoppingConstant;
import com.ztgf.shopping.models.enums.ShoppingCodeEnum;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import javax.annotation.Resource;
import com.ztgf.shopping.common.config.YmlConfig;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.shopping.mapper.epg.EpgShopMainNavigationMapper;
import com.ztgf.shopping.models.service.epg.IEpgShopMainNavigationService;
import com.ztgf.shopping.models.bo.epg.EpgShopMainNavigationSaveBO;
import com.ztgf.shopping.models.dto.epg.EpgShopMainNavigationDTO;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 店铺页主导航控制器
 *
 * @author YUE
 * @Date 2020-05-28 18:39:38
 */
@DubboService
public class EpgShopMainNavigationServiceImpl extends ServiceImpl<EpgShopMainNavigationMapper, EpgShopMainNavigationDTO> implements IEpgShopMainNavigationService{

    @Resource
    private YmlConfig ymlConfig;

    @Autowired
    private EpgShopMainNavigationMapper epgShopMainNavigationMapper;

    @Override
    public boolean add(EpgShopMainNavigationSaveBO saveBO) {
        EpgShopMainNavigationDTO dto = new EpgShopMainNavigationDTO();
        BeanUtils.copyProperties(saveBO,dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        return this.insert(dto);
    }

    /**
     * 删除功能功能相关的店铺页主导航
     * @return
     */
    public boolean deleteByModuleId(Long moduleId){
        Map<String, Object> columnMap = new HashMap<>();
        columnMap.put("module_id",moduleId);
        return epgShopMainNavigationMapper.deleteByMap(columnMap) > 0;
    }

    /**
     * 根据条件查询 店铺页主导航 有页码
     * @param epgShopMainNavigationBO
     * @return
     */
    @Override
    public Page<EpgShopMainNavigationDTO> getBySelectShopMainNavigationInfoPage(EpgShopMainNavigationBO epgShopMainNavigationBO){
        epgShopMainNavigationBO = addMustData(epgShopMainNavigationBO);
        Wrapper<EpgShopMainNavigationDTO> wrapper = getBySelectShopMainNavigationInfo(epgShopMainNavigationBO);
        return selectPage(new Page<>(epgShopMainNavigationBO.getPageNo(),epgShopMainNavigationBO.getPageSize()),wrapper);
    }

    /**
     * 根据条件查询 店铺页主导航 无页码
     * @param epgShopMainNavigationBO
     * @return
     */
    @Override
    public List<EpgShopMainNavigationDTO> getBySelectShopMainNavigationInfoNoPage(EpgShopMainNavigationBO epgShopMainNavigationBO){
        epgShopMainNavigationBO = addMustData(epgShopMainNavigationBO);
        Wrapper<EpgShopMainNavigationDTO> wrapper = getBySelectShopMainNavigationInfo(epgShopMainNavigationBO);
        if(epgShopMainNavigationBO.getPageNo() == 0 && epgShopMainNavigationBO.getPageSize() == 0)
            return epgShopMainNavigationMapper.selectPage( new Page<>(0, ShoppingConstant.SHOP_MAIN_NAVIGATION),wrapper);
        return selectList(wrapper);
    }

    /**
     * 添加必要数据
     * @param epgShopMainNavigationBO
     * @return
     */
    private EpgShopMainNavigationBO addMustData(EpgShopMainNavigationBO epgShopMainNavigationBO){
        epgShopMainNavigationBO.setIsPublish(ShoppingCodeEnum.PUBLIC_1.getCode());
        epgShopMainNavigationBO.setStatus(ShoppingCodeEnum.STATUS_0.getCode());
        epgShopMainNavigationBO.setSort(ShoppingCodeEnum.SORT_DESC.getCode());
        return epgShopMainNavigationBO;
    }

    private Wrapper<EpgShopMainNavigationDTO> getBySelectShopMainNavigationInfo(EpgShopMainNavigationBO epgShopMainNavigationBO){
        Wrapper<EpgShopMainNavigationDTO> wrapper = new EntityWrapper<>();
        EpgShopMainNavigationDTO epgShopMainNavigationDTO = new EpgShopMainNavigationDTO();
        BeanUtils.copyProperties(epgShopMainNavigationBO,epgShopMainNavigationDTO);
        wrapper.eq("status", epgShopMainNavigationBO.getStatus());
        wrapper.eq("is_publish", epgShopMainNavigationBO.getIsPublish());
        wrapper.eq("templet_id", epgShopMainNavigationBO.getTempletId());
        wrapper.eq("module_id", epgShopMainNavigationBO.getModuleId());
        wrapper.eq("shop_id", epgShopMainNavigationBO.getShopId());
        //((EntityWrapper<EpgShopMainNavigationDTO>) wrapper).setEntity(epgShopMainNavigationDTO);
        if(ShoppingCodeEnum.SORT_DESC.getCode() == epgShopMainNavigationBO.getSort().intValue())
            wrapper.orderBy("sort", false);
        else
            wrapper.orderBy("sort", true);
        return wrapper;
    }
}
